(function($) {
	var sourceData = [];
	var photoData = [];
	var dirData = [];
	var isForeground = true;
	var uid = '',locId = '',maxRole = '',nurseid=''
	mui.init({
		swipeBack: false, //启用右滑关闭功能
	});
	mui.plusReady(function() {
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = '../common/js/globalDatas.js';
		document.body.appendChild(script);
		script.onload = setup;
	});

	function setup() { //此处是原来的mui.plusReady
		//页面切换tabbar监听
		var self = plus.webview.currentWebview();
		self.setStyle({
			scrollIndicator: 'none'
		});
		mui('#allScrollWrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
			deceleration: mui.os.ios ? 0.003 : 0.0009
		});
		//共享资料库的点击
		var fileShare = document.getElementById('fileShare');
		fileShare.addEventListener('tap', elementClick, false);

		//专科小组的点击
		var professionGroup = document.getElementById('nurseGroup');
		professionGroup.addEventListener('tap', elementClick, false);

		//Gallery //临床护理 在线学习 科研动态 护理之音
		mui('#Gallery').on('tap', 'li', galleryLisTap)

		//考核量化的点击
		var kaohe = document.getElementById('kaohe');
		if (kaohe) {
			kaohe.addEventListener('tap', elementClick, false);
		}
		//通勤班车的点击
		var bus = document.getElementById('bus');
		bus.addEventListener('tap', elementClick, false);
		//协和官网的点击
		var pumch = document.getElementById('pumch');
		pumch.addEventListener('tap', elementClick, false);

		//院内资讯的点击
		mui('#newsElements').on('tap','li',newsElementsTap) 

		var perInfo = app.getState()
		if (perInfo) {
			uid = perInfo.hisUserID
			locId = perInfo.loginWardId
			maxRole = perInfo.maxRoleLevel
			nurseid = perInfo.loginID
			// console.log(JSON.stringify(perInfo));
		}
		//获取轮播图片 并监听网络变化
		var imgslider = mui("#imgSlider");
		imgslider.slider({
			interval: 1000
		});
		
		mui('#imgSlider').on('tap','.mui-slider-item',function(){
			var index = plus.webview.getWebviewById('index');
			mui.fire(index, 'toNews');
		}) 
		document.addEventListener("netchange", onNetChange, false);
	}

	mui.ready(function() {
		var slider = document.getElementById('Gallery');
		var group = slider.querySelector('.mui-slider-group');
		var items = mui('.mui-slider-item', group);
		//克隆第一个节点
		var first = items[0].cloneNode(true);
		first.classList.add('mui-slider-item-duplicate');
		//克隆最后一个节点
		var last = items[items.length - 1].cloneNode(true);
		last.classList.add('mui-slider-item-duplicate');
		//处理是否循环逻辑，若支持循环，需支持两点：
		//1、在.mui-slider-group节点上增加.mui-slider-loop类
		//2、重复增加2个循环节点，图片顺序变为：N、1、2...N、1
		var sliderApi = mui(slider).slider();

	});

	function elementClick() {
		if (this.id == 'pumch') {
			plus.runtime.openURL('http://www.pumch.cn/index.html');
			return;
		}
		var href = this.id + '.html';
		var arg = {};
		if (this.id == 'fileShare') {
			arg = {
				urlStr: '/护理健康教育共享资料库'
			};
		}else if('nurseGroup' == this.id){
			href = 'nurseGroup/'+href;
		}
		var webview = mui.openWindow({
			url: href,
			id: this.id,
			extras: arg,
			waiting: {
				autoShow: false
			}
		});
		// webview.addEventListener('close', function() {
		// 	plus.navigator.setStatusBarBackground('#fff');
		// 	plus.navigator.setStatusBarStyle('dark');
		// 	plus.navigator.setFullscreen(false);
		// }, false);
	}

	function newsElementsTap() {
		var itemId = this.id;
		var navTitle;
		var msgType = '';
		if (itemId == 'li-A') {
			navTitle = "护理新闻";
			msgType = 'hlxw';
		} else if (itemId == 'li-B') {
			navTitle = "院内网公告";
			msgType = 'ynwgg';
		}
		var webview = mui.openWindow({
			url: "newsList.html",
			extras: {
				navName: navTitle,
				msgType: msgType
			},
			waiting: {
				autoShow: false
			}
		});
		// webview.addEventListener('close', function() {
		// 	plus.navigator.setStatusBarBackground('#fff');
		// 	plus.navigator.setStatusBarStyle('dark');
		// 	plus.navigator.setFullscreen(false);
		// }, false);
	}

	function galleryLisTap() {
		var itemId = this.id;
		var index = parseInt(this.id)
		var arr = ['临床护理','在线学习','科研动态','护理之音']
		FindFileTree(arr[index]);
	}
	function FindFileTree(dirName) {
		if (dirData.length > 0) {
			getPid(dirName)
			return
		}
		var pdict = {
			'par': 0,
			'dateFlag': '0',
			'role': maxRole,
			'appFlag': '1',
			'nurseid': nurseid,
			'input': ''
		} //
		var paramDict = {
			'method': 'web.INMOrgComm:FindFileTree',
			'params': JSON.stringify(pdict),
			'type': 'MethodNoFilter'
		}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode == '1') {
				dirData = result.data
				getPid(dirName)
			} else {
				mui.toast(result.msg)
			}
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	
	function getPid(dirName) {
		var pathArr = dirName.split('/')
		var currArr = dirData
		var pid = -1
		for (var i = 0; i < pathArr.length; i++) {
			var text = pathArr[i]
			for (var j = 0; j < currArr.length; j++) {
				var dict = currArr[j]
				if (dict.label == text) { //看看需要找下级不
					if (i == pathArr.length - 1) { //最后一层
						pid = dict.id
						currArr = dict.children
						break
					} else {
						currArr = dict.children
						if (currArr == undefined) {
							break
						}
					}
					break
				}
			}
		}
		if (!currArr ) {
			if (pid == -1) {
				mui.toast('没有此文件夹,请到护管中添加')
				return 
			}
			mui.openWindow({
				url: 'fileList/hulizhiyin.html',
				id: 'fileList/hulizhiyin.html',
				extras: {'category': dirName,pid:pid},
				waiting: {
					autoShow: false, //自动显示等待框，默认为true
				}
			});
			return 
		}
		var subPage = []
		for (var i = 0; i < currArr.length; i++) {
			subPage[i] = {'title': currArr[i].label,
				id: "sub"+i+".html",
				url: "fileListSub.html",
				extras: {'category': dirName+'/'+currArr[i].label,pid:currArr[i].id}}
		}
		mui.openWindow({
			url: 'fileList/fileListP.html',
			id: 'fileList/fileListP.html',
			extras: {'subPage':subPage,'from':'tab-home.html'},
			waiting: {
				autoShow: false, //自动显示等待框，默认为true
			}
		});
	}
	document.addEventListener("pause", function() {
		isForeground = false;
	}, false);

	document.addEventListener("resume", function() {
		if (isForeground == false) {
			isForeground = true;
		}
	}, false);

	function onNetChange() {
		var nt = plus.networkinfo.getCurrentType();
		console.log('网络变化了', nt, plus.networkinfo.CONNECTION_CELL3G);
		switch (nt) {
			case plus.networkinfo.CONNECTION_ETHERNET:
				break;
			case plus.networkinfo.CONNECTION_WIFI:
				//WiFi
				console.log('wifi');
				//					photoDetective();
				break;
			case plus.networkinfo.CONNECTION_CELL3G:
				break;
			case plus.networkinfo.CONNECTION_CELL4G:
				//3G 4G
				//					photoDetective();
				break;
			default:
				//其他情况
				break;
		}
	}
	document.getElementById("footer").addEventListener('tap', function(e) {
		//photoDetective();
	});
})(mui);